package com.milhouz.quizzdroid;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.milhouz.quizzdroid.db.QuizzDroidConstants;
import com.milhouz.quizzdroid.db.QuizzDroidDbAPI;

public class SplashScreenActivity extends Activity {

	private static final int SPLASH_SCREEN_DURATION = 3000;

	private final static Handler mUiThreadCallback = new Handler();
	private Context mContext;

	/** Execute MainMenuActivity after loading */
	private final Runnable mRunAfterLoading = new Runnable() {
		@Override
		public void run() {
			Intent intent = new Intent(mContext, MainMenuActivity.class);
			startActivity(intent);
			finish();
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Set the fullscreen mode
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.splash_screen);
		mContext = this.getApplicationContext();

		// Splash screen temporisé
		mUiThreadCallback.postDelayed(mRunAfterLoading, SPLASH_SCREEN_DURATION);

		// Let's activate dithering for the background to prevent banding
		Drawable d = getResources().getDrawable(R.drawable.ss_background);
		d.setDither(true);
		findViewById(android.R.id.content).setBackgroundDrawable(d);

		// Load quiz data if not already loaded
		int nbQuestionsLoaded = QuizzDroidDbAPI.getInstance().countQuestions(this);
		if (nbQuestionsLoaded != 1000) {
			// Register default user
			QuizzDroidDbAPI.getInstance().insertDefaultUser(this);
			// Launch Question loader service
			Log.i(QuizzDroidConstants.TAG, "Execute quizz loader in ASyncTask");
			QuizzLoaderASyncTask asyncTask = new QuizzLoaderASyncTask(getApplicationContext());
			asyncTask.execute(nbQuestionsLoaded);
		}

	}

}
